﻿/********************************************************
 * Class Name     : MJournal
 * Purpose        : GL Journal Model
 * Class Used     : X_GL_Journal,DocAction 
 * Chronological    Development
 * Deepak           21-Nov-2009
  ******************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VAdvantage.Classes;
using VAdvantage.Common;
using VAdvantage.Process;
using System.Windows.Forms;
using VAdvantage.Model;
using VAdvantage.DataBase;
using VAdvantage.SqlExec;
using VAdvantage.Utility;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using VAdvantage.Logging;

namespace VAdvantage.Model
{
    public class MJournal : X_GL_Journal, DocAction
    {
        /// <summary>
        /// Standard Constructor
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="GL_Journal_ID">id</param>
        /// <param name="trxName">transaction</param>
        public MJournal(Ctx ctx, int GL_Journal_ID, String trxName)
            : base(ctx, GL_Journal_ID, trxName)
        {
            //super (ctx, GL_Journal_ID, trxName);
            if (GL_Journal_ID == 0)
            {
                //	setGL_Journal_ID (0);		//	PK
                //	setC_AcctSchema_ID (0);
                //	setC_Currency_ID (0);
                //	setC_DocType_ID (0);
                //	setC_Period_ID (0);
                //
                SetCurrencyRate(Env.ONE);
                //	setC_ConversionType_ID(0);
                SetDateAcct(DateTime.Now.Date);// Timestamp(Comm.currentTimeMillis()));
                SetDateDoc((DateTime.Now.Date));//new Timestamp(System.currentTimeMillis()));
                //	setDescription (null);
                SetDocAction(DOCACTION_Complete);
                SetDocStatus(DOCSTATUS_Drafted);
                //	setDocumentNo (null);
                //	setGL_Category_ID (0);
                SetPostingType(POSTINGTYPE_Actual);
                SetTotalCr(Env.ZERO);
                SetTotalDr(Env.ZERO);
                SetIsApproved(false);
                SetIsPrinted(false);
                SetPosted(false);
                SetProcessed(false);
            }
        }	//	MJournal

        /// <summary>
        /// Load Constructor
        /// </summary>
        /// <param name="ctx">context</param>
        /// <param name="dr">datarow</param>
        /// <param name="trxName">transaction</param>
        public MJournal(Ctx ctx, DataRow dr, String trxName)
            : base(ctx, dr, trxName)
        {
            //super(ctx, rs, trxName);
        }	//	MJournal


        public MJournal(Ctx ctx, IDataReader dr, String trxName)
            : base(ctx, dr, trxName)
        {
            //super(ctx, rs, trxName);
        }

        /// <summary>
        /// Parent Constructor.
        /// </summary>
        /// <param name="parent">parent batch</param>
        public MJournal(MJournalBatch parent)
            : this(parent.GetCtx(), 0, parent.Get_TrxName())
        {
            //this (parent.getCtx(), 0, parent.get_TrxName());
            SetClientOrg(parent);
            SetGL_JournalBatch_ID(parent.GetGL_JournalBatch_ID());
            SetC_DocType_ID(parent.GetC_DocType_ID());
            SetPostingType(parent.GetPostingType());
            //
            SetDateDoc(parent.GetDateDoc());
            SetC_Period_ID(parent.GetC_Period_ID());
            SetDateAcct(parent.GetDateAcct());
            SetC_Currency_ID(parent.GetC_Currency_ID());
        }	//	MJournal

        /// <summary>
        /// Copy Constructor.Dos not copy: Dates/Period
        /// </summary>
        /// <param name="original">original</param>
        public MJournal(MJournal original)
            : this(original.GetCtx(), 0, original.Get_TrxName())
        {
            //this (original.GetCtx(), 0, original.get_TrxName())
            SetClientOrg(original);
            SetGL_JournalBatch_ID(original.GetGL_JournalBatch_ID());
            //
            SetC_AcctSchema_ID(original.GetC_AcctSchema_ID());
            SetGL_Budget_ID(original.GetGL_Budget_ID());
            SetGL_Category_ID(original.GetGL_Category_ID());
            SetPostingType(original.GetPostingType());
            SetDescription(original.GetDescription());
            SetC_DocType_ID(original.GetC_DocType_ID());
            SetControlAmt(original.GetControlAmt());
            //
            SetC_Currency_ID(original.GetC_Currency_ID());
            SetC_ConversionType_ID(original.GetC_ConversionType_ID());
            SetCurrencyRate(original.GetCurrencyRate());

            //	setDateDoc(original.getDateDoc());
            //	setDateAcct(original.getDateAcct());
            //	setC_Period_ID(original.getC_Period_ID());
        }	//	MJournal


        /// <summary>
        /// Overwrite Client/Org if required
        /// </summary>
        /// <param name="AD_Client_ID">client</param>
        /// <param name="AD_Org_ID"> org</param>
        public new void SetClientOrg(int AD_Client_ID, int AD_Org_ID)
        {
            //super.setClientOrg(AD_Client_ID, AD_Org_ID);
            base.SetClientOrg(AD_Client_ID, AD_Org_ID);
        }	//	setClientOrg

        /// <summary>
        /// Get Period
        /// </summary>
        /// <returns>period or null</returns>
        public MPeriod GetPeriod()
        {
            int C_Period_ID = GetC_Period_ID();
            if (C_Period_ID != 0)
            {
                return MPeriod.Get(GetCtx(), C_Period_ID);
            }
            return null;
        }	//	getPeriod

        /// <summary>
        /// Set Doc Date - Callout.Sets also acct date and period
        /// </summary>
        /// <param name="oldDateDoc">old</param>
        /// <param name="newDateDoc">new</param>
        /// <param name="windowNo">window</param>
        public void SetDateDoc(String oldDateDoc,
               String newDateDoc, int windowNo)
        {
            if (newDateDoc == null || newDateDoc.Length == 0)
            {
                return;
            }
            DateTime? dateDoc = PO.ConvertToTimestamp(newDateDoc);
            if (dateDoc == null)
            {
                return;
            }
            SetDateDoc(dateDoc);
            SetDateAcct(dateDoc.Value);
        }	//	setDateDoc

        /// <summary>
        /// Set Acct Date - Callout.	Sets Period
        /// </summary>
        /// <param name="oldDateAcct">old</param>
        /// <param name="newDateAcct">new</param>
        /// <param name="windowNo">window no</param>
        public void SetDateAcct(String oldDateAcct,
               String newDateAcct, int windowNo)
        {
            if (newDateAcct == null || newDateAcct.Length == 0)
            {
                return;
            }
            DateTime? dateAcct = PO.ConvertToTimestamp(newDateAcct);
            if (dateAcct == null)
            {
                return;
            }
            SetDateAcct(dateAcct.Value);
        }	//	setDateAcct

        /// <summary>
        /// Set Period - Callout.Set Acct Date if required
        /// </summary>
        /// <param name="oldC_Period_ID">old</param>
        /// <param name="newC_Period_ID">new</param>
        /// <param name="windowNo">window no</param>
        public void SetC_Period_ID(String oldC_Period_ID,
               String newC_Period_ID, int windowNo)
        {
            if (newC_Period_ID == null || newC_Period_ID.Length == 0)
            {
                return;
            }
            int C_Period_ID = Utility.Util.GetValueOfInt(newC_Period_ID);
            if (C_Period_ID == 0)
            {
                return;
            }
            SetC_Period_ID(C_Period_ID);
        }	//	setC_Period_ID

        /// <summary>
        /// Set Accounting Date.Set also Period if not set earlier
        /// </summary>
        /// <param name="DateAcct">date</param>
        public void SetDateAcct(DateTime DateAcct)
        {
            //super.setDateAcct(DateAcct);
            base.SetDateAcct(DateAcct);
            if (GetC_Period_ID() != 0)	//	previously set
            {
                SetRate();
                return;
            }
            int C_Period_ID = MPeriod.GetC_Period_ID(GetCtx(), DateAcct);
            if (C_Period_ID == 0)
            {
                log.Warning("Period not found");
            }
            else
            {
                base.SetC_Period_ID(C_Period_ID);
                SetRate();
            }
        }	//	setDateAcct

        /// <summary>
        /// Set Period
        /// </summary>
        /// <param name="C_Period_ID">period</param>
        public new void SetC_Period_ID(int C_Period_ID)
        {
            //super.setC_Period_ID(C_Period_ID);
            base.SetC_Period_ID(C_Period_ID);
            if (C_Period_ID == 0)
            {
                return;
            }
            DateTime? dateAcct = GetDateAcct();
            //
            MPeriod period = GetPeriod();
            if (period != null)
            {
                if (period.IsStandardPeriod()
                    && !period.IsInPeriod(dateAcct))
                    base.SetDateAcct(period.GetEndDate());
            }
        }	//	setC_Period_ID


        /// <summary>
        /// Set Currency Info
        /// </summary>
        /// <param name="C_Currency_ID">currenct</param>
        /// <param name="C_ConversionType_ID">type</param>
        /// <param name="CurrencyRate">rate</param>
        public void SetCurrency(int C_Currency_ID, int C_ConversionType_ID, Decimal CurrencyRate)
        {
            if (C_Currency_ID != 0)
            {
                SetC_Currency_ID(C_Currency_ID);
            }
            if (C_ConversionType_ID != 0)
            {
                SetC_ConversionType_ID(C_ConversionType_ID);
            }
            if (CurrencyRate != null && CurrencyRate.CompareTo(Env.ZERO) == 0)
            {
                SetCurrencyRate(CurrencyRate);
            }
        }	//	setCurrency


        /// <summary>
        /// set callout
        /// </summary>
        /// <param name="oldC_ConversionType_ID">old</param>
        /// <param name="newC_ConversionType_ID">new</param>
        /// <param name="windowNo">window no</param>
        public void SetC_ConversionType_ID(String oldC_ConversionType_ID,
               String newC_ConversionType_ID, int windowNo)
        {
            if (newC_ConversionType_ID == null || newC_ConversionType_ID.Length == 0)
            {
                return;
            }
            int C_ConversionType_ID = Utility.Util.GetValueOfInt(newC_ConversionType_ID);
            if (C_ConversionType_ID == 0)
            {
                return;
            }
            SetC_ConversionType_ID(C_ConversionType_ID);
            SetRate();
        }	//	setC_ConversionType_ID

        /// <summary>
        /// set currency callout
        /// /// </summary>
        /// <param name="oldC_Currency_ID">old</param>
        /// <param name="newC_Currency_ID">new</param>
        /// <param name="windowNo">window no</param>
        public void SetC_Currency_ID(String oldC_Currency_ID,
               String newC_Currency_ID, int windowNo)
        {
            if (newC_Currency_ID == null || newC_Currency_ID.Length == 0)
            {
                return;
            }
            int C_Currency_ID = Utility.Util.GetValueOfInt(newC_Currency_ID);
            if (C_Currency_ID == 0)
            {
                return;
            }
            SetC_Currency_ID(C_Currency_ID);
            SetRate();
        }	//	setC_Currency_ID

        /// <summary>
        ///	Set Rate
        /// </summary>
        private void SetRate()
        {
            //  Source info
            int C_Currency_ID = GetC_Currency_ID();
            int C_ConversionType_ID = GetC_ConversionType_ID();
            if (C_Currency_ID == 0 || C_ConversionType_ID == 0)
            {
                return;
            }
            DateTime? DateAcct = GetDateAcct();
            if (DateAcct == null)
            {
                DateAcct = DateTime.Now;// new Timestamp(System.currentTimeMillis());
            }
            //
            int C_AcctSchema_ID = GetC_AcctSchema_ID();
            MAcctSchema a = MAcctSchema.Get(GetCtx(), C_AcctSchema_ID);
            int AD_Client_ID = GetAD_Client_ID();
            int AD_Org_ID = GetAD_Org_ID();

            Decimal? CurrencyRate = (Decimal?)MConversionRate.GetRate(C_Currency_ID, a.GetC_Currency_ID(),
                DateAcct, C_ConversionType_ID, AD_Client_ID, AD_Org_ID);
            log.Fine("rate = " + CurrencyRate);
            if (CurrencyRate.Value == null)
            {
                CurrencyRate = Env.ZERO;
            }
            SetCurrencyRate(CurrencyRate.Value);
        }	//	setRate

        /// <summary>
        /// Get Journal Lines
        /// </summary>
        /// <param name="requery">requery</param>
        /// <returns>Array of lines</returns>
        public MJournalLine[] GetLines(Boolean requery)
        {
            //ArrayList<MJournalLine> list = new ArrayList<MJournalLine>();
            List<MJournalLine> list = new List<MJournalLine>();
            String sql = "SELECT * FROM GL_JournalLine WHERE GL_Journal_ID=@Param1 ORDER BY Line";
            //PreparedStatement pstmt = null;
            SqlParameter[] Param = new SqlParameter[1];
            IDataReader idr = null;
            DataTable dt = null;
            try
            {
                //pstmt = DataBase.prepareStatement(sql, get_TrxName());
                //pstmt.setInt(1, getGL_Journal_ID());
                Param[0] = new SqlParameter("@Param1", GetGL_Journal_ID());

                idr = DataBase.DB.ExecuteReader(sql, Param, Get_TrxName());
                dt = new DataTable();
                dt.Load(idr);
                idr.Close();
                //while (rs.next())
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new MJournalLine(GetCtx(), dr, Get_TrxName()));
                }
                dt = null;
            }
            catch (Exception ex)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                if (dt != null)
                {
                    dt = null;
                }
                log.Log(Level.SEVERE, "getLines", ex);
            }
            //
            MJournalLine[] retValue = new MJournalLine[list.Count];
            //list.toArray(retValue);
            retValue = list.ToArray();
            return retValue;
        }	//	getLines

        /// <summary>
        /// Copy Lines from other Journal
        /// </summary>
        /// <param name="fromJournal">Journal</param>
        /// <param name="dateAcct">date used - if null original</param>
        /// <param name="typeCR">type of copying (C)orrect=negate - (R)everse=flip dr/cr - otherwise just copy</param>
        /// <returns>number of lines copied</returns>
        public int CopyLinesFrom(MJournal fromJournal, DateTime? dateAcct, char typeCR)
        {
            if (IsProcessed() || fromJournal == null)
            {
                return 0;
            }
            int count = 0;
            MJournalLine[] fromLines = fromJournal.GetLines(false);
            for (int i = 0; i < fromLines.Length; i++)
            {
                MJournalLine toLine = new MJournalLine(GetCtx(), 0, fromJournal.Get_TrxName());
                PO.CopyValues(fromLines[i], toLine, GetAD_Client_ID(), GetAD_Org_ID());
                toLine.SetGL_Journal_ID(GetGL_Journal_ID());
                //
                if (dateAcct != null)
                {
                    toLine.SetDateAcct(dateAcct);
                }
                //	Amounts
                if (typeCR == 'C')			//	correct
                {
                    // toLine.SetAmtSourceDr(fromLines[i].GetAmtSourceDr().negate());
                    toLine.SetAmtSourceDr(Decimal.Negate(fromLines[i].GetAmtSourceDr()));
                    toLine.SetAmtSourceCr(Decimal.Negate(fromLines[i].GetAmtSourceCr()));//.negate());
                }
                else if (typeCR == 'R')		//	reverse
                {
                    toLine.SetAmtSourceDr(fromLines[i].GetAmtSourceCr());
                    toLine.SetAmtSourceCr(fromLines[i].GetAmtSourceDr());
                }
                toLine.SetIsGenerated(true);
                toLine.SetProcessed(false);
                if (toLine.Save())
                {
                    count++;
                }
            }
            if (fromLines.Length != count)
            {
                log.Log(Level.SEVERE, "Line difference - JournalLines=" + fromLines.Length + " <> Saved=" + count);
            }

            return count;
        }	//	copyLinesFrom

        /// <summary>
        /// Set Processed.
        /// </summary>
        /// <param name="processed">Propergate to Lines/Taxes</param>
        public void SetProcessed(Boolean processed)
        {
            //super.setProcessed (processed);
            base.SetProcessed(processed);
            if (Get_ID() == 0)
            {
                return;
            }
            String sql = "UPDATE GL_JournalLine SET Processed='"
                + (processed ? "Y" : "N")
                + "' WHERE GL_Journal_ID=" + GetGL_Journal_ID();
            int noLine = DataBase.DB.ExecuteQuery(sql, null, Get_TrxName());
            log.Fine(processed + " - Lines=" + noLine);
        }	//	setProcessed


        /// <summary>
        /// Before Save
        /// </summary>
        /// <param name="newRecord">new</param>
        /// <returns>true</returns>
        protected override Boolean BeforeSave(Boolean newRecord)
        {
            //	Imported Journals may not have date
            if (GetDateDoc() == null)
            {
                if (GetDateAcct() == null)
                {
                    //SetDateDoc(new Timestamp(System.currentTimeMillis()));
                    SetDateDoc(DateTime.Now);
                }
                else
                {
                    SetDateDoc(GetDateAcct());
                }
            }
            if (GetDateAcct() == null)
            {
                SetDateAcct(GetDateDoc());
            }
            return true;
        }	//	beforeSave


        /// <summary>
        /// After Save.
        /// </summary>
        /// <param name="newRecord">true if new record</param>
        /// <param name="success">true if success</param>
        /// <returns>success</returns>
        protected override Boolean AfterSave(Boolean newRecord, Boolean success)
        {
            if (!success)
            {
                return success;
            }
            return UpdateBatch();
        }	//	afterSave

        /// <summary>
        /// After Delete
        /// </summary>
        /// <param name="success"> true if deleted</param>
        /// <returns> true if success</returns>
        protected override Boolean AfterDelete(Boolean success)
        {
            if (!success)
            {
                return success;
            }
            return UpdateBatch();
        }	//	afterDelete

        /// <summary>
        /// Update Batch total
        /// </summary>
        /// <returns>true if ok</returns>
        private Boolean UpdateBatch()
        {
            String sql = "UPDATE GL_JournalBatch jb"
                + " SET (TotalDr, TotalCr) = (SELECT COALESCE(SUM(TotalDr),0), COALESCE(SUM(TotalCr),0)" //jz hard coded ", "
                    + " FROM GL_Journal j WHERE j.IsActive='Y' AND jb.GL_JournalBatch_ID=j.GL_JournalBatch_ID) "
                + "WHERE GL_JournalBatch_ID=" + GetGL_JournalBatch_ID();
            int no = DataBase.DB.ExecuteQuery(sql, null, Get_TrxName());
            if (no != 1)
            {
                log.Warning("afterSave - Update Batch #" + no);
            }
            return no == 1;
        }	//	updateBatch


        /// <summary>
        ///	Process document
        /// </summary>
        /// <param name="processAction"> document action</param>
        /// <returns>true if performed</returns>
        public Boolean ProcessIt(String processAction)
        {
            m_processMsg = null;
            DocumentEngine engine = new DocumentEngine(this, GetDocStatus());
            return engine.ProcessIt(processAction, GetDocAction());
        }	//	process

        /**	Process Message 			*/
        private String m_processMsg = null;
        /**	Just Prepared Flag			*/
        private Boolean m_justPrepared = false;

        /// <summary>
        ///	Unlock Document.
        /// </summary>
        /// <returns> true if success </returns>
        public Boolean UnlockIt()
        {
            log.Info(ToString());
            SetProcessing(false);
            return true;
        }	//	unlockIt

        /// <summary>
        /// Invalidate Document
        /// </summary>
        /// <returns> true if success </returns>
        public Boolean InvalidateIt()
        {
            log.Info(ToString());
            return true;
        }	//	invalidateIt

        /// <summary>
        /// Prepare Document
        /// </summary>
        /// <returns>new status (In Progress or Invalid) </returns>
        public String PrepareIt()
        {
            log.Info(ToString());
            m_processMsg = ModelValidationEngine.Get().FireDocValidate(this, ModalValidatorVariables.DOCTIMING_BEFORE_PREPARE);
            if (m_processMsg != null)
            {
                return DocActionVariables.STATUS_INVALID;
            }
            MDocType dt = MDocType.Get(GetCtx(), GetC_DocType_ID());

            //	Get Period
            MPeriod period = MPeriod.Get(GetCtx(), GetDateAcct());
            if (period == null)
            {
                log.Warning("No Period for " + GetDateAcct());
                m_processMsg = "@PeriodNotFound@";
                return DocActionVariables.STATUS_INVALID;
            }
            //	Standard Period
            if (period.GetC_Period_ID() != GetC_Period_ID()
                && period.IsStandardPeriod())
            {
                m_processMsg = "@PeriodNotValid@";
                return DocActionVariables.STATUS_INVALID;
            }
            Boolean open = period.IsOpen(dt.GetDocBaseType());
            if (!open)
            {
                log.Warning(period.GetName()
                    + ": Not open for " + dt.GetDocBaseType() + " (" + GetDateAcct() + ")");
                m_processMsg = "@PeriodClosed@";
                return DocActionVariables.STATUS_INVALID;
            }

            //	Lines
            MJournalLine[] lines = GetLines(true);
            if (lines.Length == 0)
            {
                m_processMsg = "@NoLines@";
                return DocActionVariables.STATUS_INVALID;
            }

            //	Add up Amounts
            Decimal AmtSourceDr = Env.ZERO;
            Decimal AmtSourceCr = Env.ZERO;
            for (int i = 0; i < lines.Length; i++)
            {
                MJournalLine line = lines[i];
                if (!IsActive())
                {
                    continue;
                }
                //
                if (line.IsDocControlled())
                {
                    m_processMsg = "@DocControlledError@ - @Line@=" + line.GetLine()
                        + " - " + line.GetAccountElementValue();
                    return DocActionVariables.STATUS_INVALID;
                }
                //
                AmtSourceDr = Decimal.Add(AmtSourceDr, line.GetAmtSourceDr());
                AmtSourceCr = Decimal.Add(AmtSourceCr, line.GetAmtSourceCr());
            }
            SetTotalDr(AmtSourceDr);
            SetTotalCr(AmtSourceCr);

            //	Control Amount
            if (Env.ZERO.CompareTo(GetControlAmt()) != 0
                && GetControlAmt().CompareTo(GetTotalDr()) != 0)
            {
                m_processMsg = "@ControlAmtError@";
                return DocActionVariables.STATUS_INVALID;
            }

            //	Unbalanced Jornal & Not Suspense
            if (AmtSourceDr.CompareTo(AmtSourceCr) != 0)
            {
                MAcctSchemaGL gl = MAcctSchemaGL.Get(GetCtx(), GetC_AcctSchema_ID());
                if (gl == null || !gl.IsUseSuspenseBalancing())
                {
                    m_processMsg = "@UnbalancedJornal@";
                    return DocActionVariables.STATUS_INVALID;
                }
            }

            if (!DOCACTION_Complete.Equals(GetDocAction()))
                SetDocAction(DOCACTION_Complete);
            m_justPrepared = true;
            return DocActionVariables.STATUS_INPROGRESS;
        }	//	prepareIt

        /// <summary>
        /// Approve Document
        /// </summary>
        /// <returns>true if success </returns>
        public Boolean ApproveIt()
        {
            log.Info(ToString());
            SetIsApproved(true);
            return true;
        }	//	approveIt

        /// <summary>
        /// Reject Approval
        /// </summary>
        /// <returns> true if success </returns>
        public Boolean RejectIt()
        {
            log.Info(ToString());
            SetIsApproved(false);
            return true;
        }	//	rejectIt

        /// <summary>
        ///	Complete Document
        /// </summary>
        /// <returns>new status (Complete, In Progress, Invalid, Waiting ..)</returns>
        public String CompleteIt()
        {
            //	Re-Check
            if (!m_justPrepared)
            {
                String status = PrepareIt();
                if (!DocActionVariables.STATUS_INPROGRESS.Equals(status))
                {
                    return status;
                }
            }
            //	Implicit Approval
            if (!IsApproved())
            {
                ApproveIt();
            }
            log.Info(ToString());
            //	User Validation
            String valid = ModelValidationEngine.Get().FireDocValidate(this, ModalValidatorVariables.DOCTIMING_AFTER_COMPLETE);
            if (valid != null)
            {
                m_processMsg = valid;
                return DocActionVariables.STATUS_INVALID;
            }
            //
            SetProcessed(true);
            SetDocAction(DOCACTION_Close);
            return DocActionVariables.STATUS_COMPLETED;
        }	//	completeIt

        /// <summary>
        ///	Void Document.
        /// </summary>
        /// <returns>true if success </returns>
        public Boolean VoidIt()
        {
            log.Info(ToString());
            if (DOCSTATUS_Drafted.Equals(GetDocStatus())
                || DOCSTATUS_Invalid.Equals(GetDocStatus()))
            {
                SetProcessed(true);
                SetDocAction(DOCACTION_None);
                return true;
            }
            return false;
        }	//	voidIt

        /// <summary>
        /// Close Document.Cancel not delivered Qunatities
        /// </summary>
        /// <returns>true if success </returns>
        public Boolean CloseIt()
        {
            log.Info(ToString());
            if (DOCSTATUS_Completed.Equals(GetDocStatus()))
            {
                SetProcessed(true);
                SetDocAction(DOCACTION_None);
                return true;
            }
            return false;
        }	//	closeIt

        /// <summary>
        /// Reverse Correction (in same batch).As if nothing happened - same date
        /// </summary>
        /// <returns> true if success </returns>
        public Boolean ReverseCorrectIt()
        {
            return ReverseCorrectIt(GetGL_JournalBatch_ID()) != null;
        }	//	reverseCorrectIt

        /// <summary>
        /// Reverse Correction.As if nothing happened - same date
        /// </summary>
        /// <param name="GL_JournalBatch_ID">batch</param>
        /// <returns>reversed journal or null</returns>
        public MJournal ReverseCorrectIt(int GL_JournalBatch_ID)
        {
            log.Info(ToString());
            //	Journal
            MJournal reverse = new MJournal(this);
            reverse.SetGL_JournalBatch_ID(GL_JournalBatch_ID);
            reverse.SetDateDoc(GetDateDoc());
            reverse.SetC_Period_ID(GetC_Period_ID());
            reverse.SetDateAcct(GetDateAcct());
            //	Reverse indicator
            String description = reverse.GetDescription();
            if (description == null)
            {
                description = "** " + GetDocumentNo() + " **";
            }
            else
            {
                description += " ** " + GetDocumentNo() + " **";
            }
            reverse.SetDescription(description);
            if (!reverse.Save())
            {
                return null;
            }

            //	Lines
            reverse.CopyLinesFrom(this, null, 'C');
            //
            SetProcessed(true);
            SetDocAction(DOCACTION_None);
            return reverse;
        }	//	reverseCorrectionIt

        /// <summary>
        /// Reverse Accrual (sane batch).	Flip Dr/Cr - Use Today's date
        /// </summary>
        /// <returns> true if success </returns>
        public Boolean ReverseAccrualIt()
        {
            return ReverseAccrualIt(GetGL_JournalBatch_ID()) != null;
        }	//	reverseAccrualIt

        /// <summary>
        ///	Reverse Accrual.	Flip Dr/Cr - Use Today's date
        /// </summary>
        /// <param name="GL_JournalBatch_ID">reversal batch</param>
        /// <returns> journal or null </returns>
        public MJournal ReverseAccrualIt(int GL_JournalBatch_ID)
        {
            log.Info(ToString());
            //	Journal
            MJournal reverse = new MJournal(this);
            reverse.SetGL_JournalBatch_ID(GL_JournalBatch_ID);
            reverse.SetDateDoc(DateTime.Now);
            reverse.Set_ValueNoCheck("C_Period_ID", null);		//	reset
            reverse.SetDateAcct(reverse.GetDateDoc());
            //	Reverse indicator
            String description = reverse.GetDescription();
            if (description == null)
            {
                description = "** " + GetDocumentNo() + " **";
            }
            else
            {
                description += " ** " + GetDocumentNo() + " **";
            }
            reverse.SetDescription(description);
            if (!reverse.Save())
            {
                return null;
            }

            //	Lines
            reverse.CopyLinesFrom(this, reverse.GetDateAcct(), 'R');
            //
            SetProcessed(true);
            SetDocAction(DOCACTION_None);
            return reverse;
        }	//	reverseAccrualIt

        /// <summary>
        /// Re-activate
        /// </summary>
        /// <returns>true if success </returns>
        public Boolean ReActivateIt()
        {
            log.Info(ToString());
            return false;
        }	//	reActivateIt


        /// <summary>
        /// Get Summary
        /// </summary>
        /// <returns>Summary of Document</returns>
        public String GetSummary()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(GetDocumentNo());
            //	: Total Lines = 123.00 (#1)
            sb.Append(": ")
                .Append(Msg.Translate(GetCtx(), "TotalDr")).Append("=").Append(GetTotalDr())
                .Append(" ")
                .Append(Msg.Translate(GetCtx(), "TotalCR")).Append("=").Append(GetTotalCr())
                .Append(" (#").Append(GetLines(false).Length).Append(")");
            //	 - Description
            if (GetDescription() != null && GetDescription().Length > 0)
            {
                sb.Append(" - ").Append(GetDescription());
            }
            return sb.ToString();
        }	//	getSummary

        /// <summary>
        /// String Representation
        /// </summary>
        /// <returns> info</returns>
        public override String ToString()
        {
            StringBuilder sb = new StringBuilder("MJournal[");
            sb.Append(Get_ID()).Append(",").Append(GetDescription())
                .Append(",DR=").Append(GetTotalDr())
                .Append(",CR=").Append(GetTotalCr())
                .Append("]");
            return sb.ToString();
        }	//	toString

        /// <summary>
        /// Get Document Info

        /// </summary>
        /// <returns>document info (untranslated)</returns>
        public String GetDocumentInfo()
        {
            MDocType dt = MDocType.Get(GetCtx(), GetC_DocType_ID());
            return dt.GetName() + " " + GetDocumentNo();
        }	//	getDocumentInfo

        /// <summary>
        /// Create PDF
        /// </summary>
        /// <returns>File or null</returns>
        public FileInfo CreatePDF()
        {
            try
            {
                string fileName = Get_TableName() + Get_ID() + "_" + CommonFunctions.GenerateRandomNo()
                                    + ".pdf"; //.pdf
                string filePath = Path.GetTempPath() + fileName;

                FileInfo temp = new FileInfo(filePath);
                if (!temp.Exists)
                {
                    return CreatePDF(temp);
                }
            }
            catch (Exception e)
            {
                log.Severe("Could not create PDF - " + e.Message);
            }
            return null;
        }
        /**
         * 	Create PDF file
         *	@param file output file
         *	@return file if success
         */
        public FileInfo CreatePDF(FileInfo file)
        {
            //	ReportEngine re = ReportEngine.get (getCtx(), ReportEngine.INVOICE, getC_Invoice_ID());
            //	if (re == null)
            return null;
            //	return re.getPDF(file);
        }	//	createPDF


        /// <summary>
        /// Get Process Message
        /// </summary>
        /// <returns>clear text error message</returns>
        public String GetProcessMsg()
        {
            return m_processMsg;
        }	//	getProcessMsg

        /// <summary>
        /// Get Document Owner (Responsible)
        /// </summary>
        /// <returns>AD_User_ID (Created)</returns>
        public int GetDoc_User_ID()
        {
            return GetCreatedBy();
        }	//	getDoc_User_ID

        /// <summary>
        /// Get Document Approval Amount
        /// </summary>
        /// <returns>DR amount</returns>
        public Decimal GetApprovalAmt()
        {
            return GetTotalDr();
        }

        #region DocAction Members


        public Env.QueryParams GetLineOrgsQueryInfo()
        {
            return null;
        }

        public DateTime? GetDocumentDate()
        {
            return null;
        }

        public string GetDocBaseType()
        {
            return null;
        }

        public Trx Get_Trx()
        {
            return null;
        }

        public void SetProcessMsg(string processMsg)
        {

        }



        #endregion

    }	//	MJournal

}
